<template>
    <div class="observerLibrary-increase">
        <!-- 新增弹窗 -->
        <a-modal
         :visible="visibleShow" 
         :title="addEditParameter.id?'编辑':'新增'" 
         @ok="handleOk" 
         :confirmLoading="confirmLoading"
         @cancel="visibleShow = false">
            <a-spin size="large" :spinning="spinning">
                <a-form v-if="visibleShow" :form="form" :label-col="{ span: 5 }" :wrapper-col="{ span: 12 }" @submit="handleSubmit">
                    <a-form-item label="前缀">
                        <a-input
                        placeholder="请输入前缀"
                        v-decorator="[
                            'fqz', { initialValue:formState.fqz, rules: rules.fqz}
                        ]"/>
                    </a-form-item>
                    <a-form-item label="后缀">
                        <a-input
                        placeholder="请输入后缀"
                        v-decorator="['fhz', { initialValue:formState.fhz, rules: rules.fhz}]" />
                    </a-form-item>
                    <a-form-item label="流水号位数">
                        <a-input-number id="inputNumber" v-model="formState.flshws" :min="1" @change="forderChange" />
                    </a-form-item>
                    <a-form-item label="类型1">
                        <a-input
                        placeholder="请输入类型1"
                        v-decorator="['ftype1', { initialValue:formState.ftype1, rules: rules.ftype1}]" />
                    </a-form-item>
                    <a-form-item label="类型2">
                        <a-input
                        placeholder="请输入类型2"
                        v-decorator="['ftype2', { initialValue:formState.ftype2, rules: rules.ftype2}]" />
                    </a-form-item>
                    <a-form-item label="类型3">
                        <a-input
                        placeholder="请输入类型3"
                        v-decorator="['ftype3', { initialValue:formState.ftype3, rules: rules.ftype3}]" />
                    </a-form-item>
                    <a-form-item label="类型4">
                        <a-input
                        placeholder="请输入类型4"
                        v-decorator="['ftype4', { initialValue:formState.ftype4, rules: rules.ftype4}]" />
                    </a-form-item>
                </a-form>
            </a-spin>
        </a-modal>
    </div>
</template>
<script>
import {typeInput, typeNumber, typeSelect, typeCheckbox, typeRadio} from '@/components/compSearchCriteria/ContractType'
import compSelector from '@/components/compSelector/compSelector'
import validator from '@/common/validator'
import {addServe, editServe,ocalityPostAction} from './service/index'
export default {
    components:{
        compSelector
    },
    props:{
        addEditParameter:{
            type:Object,
            default:[]
        }
    },
    data(){
        return{
            spinning:true,
            form: this.$form.createForm(this, { name: 'coordinated' }),
            visibleShow:false,
            confirmLoading:false,
            typeSelect:typeSelect,
            formState:{},
            rules:{
                fqz: [{
                    required: true,
                    message: '请输入前缀',
                    trigger: 'blur',
                }],
                fhz: [{
                    required: true,
                    message: '请输入后缀',
                    trigger: 'blur',
                }],
                ftype1: [{
                    required: true,
                    message: '请输入类型1',
                    trigger: 'blur',
                }],
                ftype2: [{
                    required: true,
                    message: '请输入类型2',
                    trigger: 'blur',
                }],
                ftype3: [{
                    required: true,
                    message: '请输入类型3',
                    trigger: 'blur',
                }],
                ftype4: [{
                    required: true,
                    message: '请输入类型4',
                    trigger: 'blur',
                }],
                
            },
            formRequer:'',
            optionsArr:[],
            submittedSuccessfully:false,
        }
    },
    watch:{
        addEditParameter(newval,oldVal){
            this.spinning = true
            //console.log('给了什么啊',newval)
            this.formState = {
                fhz:newval.fhz?newval.fhz:'',
                fqz:newval.fqz?newval.fqz:'',
                ftype1:newval.ftype1?newval.ftype1:'',
                ftype2:newval.ftype2?newval.ftype2:'',
                ftype3:newval.ftype3?newval.ftype3:'',
                ftype4:newval.ftype4?newval.ftype4:'',
                flshws:newval.flshws?Number(newval.flshws):1,
            }
            if(newval.id){
                this.formState.id = newval.id
            }
        }
    },
    mounted(){
        if(this.addEditParameter.id){
            this.formState.id = this.addEditParameter.id
        }
        
    },
    updated(){
        this.cancelLoading()
    },
    methods:{
        cancelLoading(){
            let timer = setInterval((val)=>{
                this.spinning = false
                clearInterval(timer);
            },500)
            
        },
        forderChange(val){
            //console.log(val)
            if(!val){
                this.formState.flshws = 1
            }
        },
        handleSubmit(e) {
            // e.preventDefault();
            var formState = {}
            this.form.validateFields((err, values) => {
                if (!err) {
                    this.confirmLoading = true
                    formState = {
                        ...this.formState,
                        ...values
                    }
                    if(formState.id){
                        editServe(formState).then(res=>{
                            if(res.code == 200){
                                this.submittedSuccessfully = true
                                this.$message.success('修改成功')
                                this.confirmLoading = false
                                this.visibleShow = false
                                this.$emit('handleOk',true,this.formState.id)
                            }else{
                                this.$message.warning(res.message)
                                this.confirmLoading = false
                            }
                        })
                    }else{
                        addServe(formState).then(res=>{
                            if(res.code == 200){
                                this.$message.success('添加成功')
                                
                                this.submittedSuccessfully = true
                                this.confirmLoading = false
                                this.visibleShow = false
                                this.$emit('handleOk',true,'')
                            }else{
                                this.$message.warning(res.message)
                                this.confirmLoading = false
                            }
                        })
                    }
                    
                }
            });
            
        },
        handleOk(){
            
            this.handleSubmit()
        }
    }

}

</script>
<style>
/* .ant-form-item-label{
    font-weight: 600;
} */

 .ant-modal-title{
    font-weight: 600;
 }
 .ant-modal-header{
    background: #5ca3e5;
 }
 .ant-upload-list{
    display: none !important;
 }
 .ant-modal-footer .ant-btn-primary{
    border: 1px solid #5ca3e5 !important;
    background-color: #5ca3e5 !important;
    color: #fff !important;
 }
</style>